/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

public class IdentifyRegistration extends ItemDefinition {

	private Loci loci = null;
	private Func func1 = null;
	private Func func2 = null;
	private Argeqs argeqs = null;
	
	private IdentifyRegistration(){
		super(null);
	}

	public IdentifyRegistration(Loci loci, Func func1, Func func2, Argeqs argeqs) throws DliException {
		super(null);
		if (loci == null || func1 == null || func2 == null || argeqs == null){
			throw new DliException();
		}
		this.loci = loci;
		this.func1 = func1;
		this.func2 = func2;
		this.argeqs = argeqs;
    }

	public static ItemDefinition newInstance(String string) throws DliParseException {
		IdentifyRegistration identifyRegistration = new IdentifyRegistration();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in IdentifyRegistration");
		}
		if (!string.substring(0, position).equals("idreg")) {
			throw new DliParseException("["+string+"] is not valid in IdentifyRegistration");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in IdentifyRegistration");
		}
		if (string.startsWith("$loci")){
			identifyRegistration.loci = Loci.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1);
		} else {
			throw new DliParseException("["+string+"] is not valid in IdentifyRegistration");
		}
		int positionFunc = string.indexOf(":func ");
		if (positionFunc != -1) {
			positionComma = Utility.searchSeparationComma(string);
			if (positionComma == -1) {
				throw new DliParseException("["+string+"] is not valid in IdentifyRegistration");
			}
			identifyRegistration.func1 = Func.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1);
		} else {
			throw new DliParseException("["+string+"] is not valid in IdentifyRegistration");
		}
		positionFunc = string.indexOf(":func ");
		if (positionFunc != -1) {
			positionComma = Utility.searchSeparationComma(string);
			if (positionComma == -1) {
				throw new DliParseException("["+string+"] is not valid in IdentifyRegistration");
			}
			identifyRegistration.func2 = Func.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1);
		} else {
			throw new DliParseException("["+string+"] is not valid in IdentifyRegistration");
		}
		identifyRegistration.argeqs = Argeqs.newInstance(string);
		return identifyRegistration;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("idreg(");
		sb.append(loci.toString());
		sb.append(",");
		sb.append(func1.toString());
		sb.append(",");
		sb.append(func2.toString());
		sb.append(",");
		sb.append(argeqs.toString());
		sb.append(")");
		return sb.toString();
		
	}
	
}
